package com.example.dragcontentviewdemo.activity.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.dragcontentviewdemo.R;

import java.util.Random;

public class DemoSimpleListViewAdapter extends BaseAdapter{

    private Random random = new Random(System.currentTimeMillis());
    private int[] data;
    private int count = 0;

    public DemoSimpleListViewAdapter(int itemCount){
        count = itemCount;
        data = new int[count];
        for (int i = 0; i < count; i++) {
            data[i] = random.nextInt();
        }
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public Object getItem(int position) {
        return data[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if ( convertView != null ) {
            return  convertView;
        }
        //inflate layout
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        convertView = inflater.inflate(R.layout.listview_item_layout,parent,false);

        //findView and configure
        //convertView被回收后会产生新的数据
        int randomNumber = data[position];
        TextView textView = convertView.findViewById(R.id.text);
        textView.setText(Integer.toString(randomNumber));
        View bubbleView = convertView.findViewById(R.id.bubble);
        addDragBehaviourToView(bubbleView);

        return convertView;
    }

    private void addDragBehaviourToView(View view){
        view.setOnTouchListener(new DragGestureListenerImpl());
    }


}
